"""
    栈的顺序存储结构
"""


# 自定义异常
class StackError(Exception):
    pass


class MyStack:
    def __init__(self):
        # 约定列表的最后一个元素为栈顶
        self._elems = []

    def top(self):
        if not self._elems:
            raise StackError("stack is empty")

        return self._elems[-1]

    def is_empty(self):
        """
            判断栈是否为空
        :return:
        """
        return self._elems == []

    def push(self, elem):
        """
            入栈
        :param elem:
        :return:
        """
        self._elems.append(elem)

    def pop(self):
        """
            出栈
        :return:
        """
        if not self._elems:
            raise StackError('stack is empty')
        return self._elems.pop()


if __name__ == "__main__":
    st = MyStack()  # 初始化
    # print(st.top())
    print(st.is_empty())
    st.push(10)
    st.push(20)
    st.push(30)
    while not st.is_empty():
        print(st.pop())
